package cjlu.ea.h.controller;


import cjlu.ea.h.common.R;
import cjlu.ea.h.entity.DBsyxwsqb;
import cjlu.ea.h.entity.DBysfxshb;
import cjlu.ea.h.service.F103DBysfxshbService;
import cjlu.ea.h.service.SUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-07-03
 */
@RestController
@CrossOrigin
@RequestMapping("/h/d_bysfxshb")
public class F103DBysfxshbController {
    @Autowired
    F103DBysfxshbService DBysfxshbService;
    @Autowired
    SUserService userService;

    //获取毕业生数据
    @GetMapping("/getbyssj")
    public R getbyssj(){
        List<DBysfxshb> list = DBysfxshbService.list(null);
        System.out.println(list);
        return R.ok().data("list",list);
    }

    @PostMapping("/getPagebyssjBytj/{current}/{limit}")
    public R getPagebyssjBytj(@PathVariable Long current, @PathVariable Long limit,
                              @RequestBody(required = false) DBysfxshb dBysfxshb, HttpServletRequest request){


        String roles = userService.getUserRoles(userService.getToken(request));
        List<Object> res;
        if(roles.equals("校级")){
            res = DBysfxshbService.getxfsxbys(dBysfxshb,current,limit,null);
        }else{
            res = DBysfxshbService.getxfsxbys(dBysfxshb,current,limit,roles);
        }

        return R.ok().data("list",(List<DBysfxshb>)res.get(0)).data("total",(Long)res.get(1));
    }

    @GetMapping("/getAllPagebyssjBytj/{current}/{limit}")
    public R getAllPagebyssjBytj(@PathVariable Long current,@PathVariable Long limit,HttpServletRequest request){

        String roles = userService.getUserRoles(userService.getToken(request));
        List<DBysfxshb> list;
        Page<DBysfxshb> page;
        //分页显示
        if(roles.equals("校级")){
            page = new Page<>(current,limit);
            DBysfxshbService.page(page,null);
        }else{
            QueryWrapper wrapper = new QueryWrapper();
            wrapper.eq("xy",roles);
            page = new Page<>(current,limit);
            DBysfxshbService.page(page,wrapper);
        }

        //数据查询获取
        list = page.getRecords();
        Long total = page.getTotal();

        return R.ok().data("list",list).data("total",total);
    }


    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id){

        boolean result = DBysfxshbService.removeById(id);
        if (result){
            return R.ok().message("删除成功");
        }else{
            return R.error().message("删除失败");
        }

    }

    @GetMapping("getbysById/{id}")
    public R getbysById(@PathVariable Integer id){
        return R.ok().data("bys", DBysfxshbService.getById(id));
    }

    @PostMapping("update")
    public R updatebysById(@RequestBody DBysfxshb dBysfxshb){
        DBysfxshb newbys = new DBysfxshb();
        BeanUtils.copyProperties(dBysfxshb,newbys);
        List<DBysfxshb> list = new ArrayList<>();
        list.add(newbys);
        DBysfxshbService.saveOrUpdateBatch(list);
        return R.ok();
    }

    //新增按钮
    @PostMapping("saveOrUpdateOne")
    public R saveOrUpdateBysById(@RequestBody DBysfxshb dBysfxshb){
        DBysfxshb newbys = new DBysfxshb();
        BeanUtils.copyProperties(dBysfxshb,newbys);
        List<DBysfxshb> list = new ArrayList<>();
        list.add(newbys);
        DBysfxshbService.saveOrUpdateBatch(list);
        return R.ok();
    }



}

